package com._51cto.healthy.entity;

import com._51cto.healthy.excel.ExcelCheckItem;
import com._51cto.healthy.pojo.CheckItem;
import com._51cto.healthy.service.CheckItemService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ExcelCheckItemListener implements ReadListener<ExcelCheckItem> {

    private CheckItemService checkItemService;

    private List<CheckItem> list = new ArrayList<>();

    public ExcelCheckItemListener(CheckItemService checkItemService){
        this.checkItemService = checkItemService;
    }

    @Override
    public void invoke(ExcelCheckItem excelCheckItem, AnalysisContext analysisContext) {
        String json = JSONObject.toJSONString(excelCheckItem);
        //把实体转为json格式
        CheckItem item = JSONObject.parseObject(json,CheckItem.class);
        //日期格式转换
        item.setSex("男".equals(excelCheckItem.getSex()) ? "1" : "女".equals(excelCheckItem.getSex()) ? "2" : "0");
        item.setType("检查".equals(excelCheckItem.getType()) ? "1" : "2");
        list.add(item);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        checkItemService.addList(list);
    }
}
